<?php
class PermissionController extends BaseController {

    public function check() {
        $m = new PermissionModel();
        $input = $this->getInput('post');
        try {
            $m->check($input);
        } catch (ExceptionLib $e) {
            $data = array(
                'msg' => $e->getMessage(),
            );
            switch($e->getCode()) {
                case 3 :
                    $data['title'] = '尚未在此地区提供服务';
                    break;
                case 4 :
                    $data['title'] = '未滿18歲不得瀏覽';
                    break;
            }
            throw new ControllerExceptionLib(array(
                'jump' => false,
                'code' => '403',
                'tpl' => '403',
                'data' => $data,
                )
            );
        }
        setcookie('is_allowed', '1', time() + 3600*29, '/');
        $uri = '/books/';
        if(!empty($input['from'])) {
            $uri = rawurldecode($input['from']);
        }
        return array(
            'jump' => true,
            'uri' => $uri,
            'code' => '302',
        );
    }

    public function index() {
        $input = $this->getInput('get');
        if(isset($input['i']) && $input['i'] == 'cc') {
            setcookie('is_allowed', '1', time() + 3600*29, '/');
            return array(
                'jump' => true,
                'uri' => '/books/',
                'code' => '302',
            );

        } else {
            setcookie('is_allowed', '0', time() - 3600*29, '/');
        }
        $data = array();
        if(stripos($_SERVER['HTTP_ACCEPT_LANGUAGE'], 'zh') == 0) {
            $data['location'] = 'cn';
        } else {
            $data['location'] = 'us';
        }
        return array(
            'tpl' => 'permission/index',
            'data' => $data,
        );
    }
}
?>
